package com.seari.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.DeviceType;
import com.seari.bean.DeviceTypeAttr;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryDeviceType;
import com.seari.service.DeviceTypeAttrService;
import com.seari.service.DeviceTypeService;
import com.seari.utils.StringHelper;
import org.springframework.util.CollectionUtils;

@Controller
@RequestMapping("dp/")
public class DeviceTypeController
{
	@Autowired
	private DeviceTypeService dts;

	@Autowired
	private DeviceTypeAttrService dtas;

	@RequestMapping(value = "getDeviceType")
	@ResponseBody
	public DataGridBean<DeviceType> getDeviceType(HttpServletRequest req)
	{
		DataGridBean<DeviceType> grid = new DataGridBean<DeviceType>();
		int pageNum = Integer.parseInt(req.getParameter("page"));
		int rows = Integer.parseInt(req.getParameter("rows"));
		// QueryDeviceList qdl = new QueryDeviceList();
		QueryDeviceType qdt = new QueryDeviceType();
		String line = (String) req.getSession().getAttribute("LineID");
		qdt.setLine(line);
		qdt.setStart(rows * (pageNum - 1));
		qdt.setLimit(rows);
		List<DeviceType> deviceList = dts.getDeviceTypeByParams(qdt);
		grid.setRows(deviceList);
		int total = dts.getDeviceTypeCount(qdt);
		grid.setTotal(total);
		return grid;
	}

	@RequestMapping(value = "addDeviceTypeByModel")
	@ResponseBody
	public MessageBean addDeviceTypeByModel(HttpServletRequest req)
	{
		MessageBean mb = new MessageBean();
		String deviceName = req.getParameter("deviceName");
		String targetName = req.getParameter("targetName");
		QueryDeviceType qdt = new QueryDeviceType();
		String line = (String) req.getSession().getAttribute("LineID");
		qdt.setLine(line);

		qdt.setDeviceName(deviceName);
		List<DeviceType> lists = dts.getDeviceTypeByParams(qdt);
		if (CollectionUtils.isEmpty(lists))
		{
			mb.setType(StringHelper.MESSAGE_TYPE_ERROR);
			mb.setMessage("数据库中无此类名");
			return mb;
		}
		DeviceType deviceType = lists.get(0);
		deviceType.setDeviceName(targetName);
		Integer result = dts.getDeviceIdByName(targetName, line);
		if (null != result && result > 0)
		{
			// 已存在此条记录
			deviceType.setTid(result);
		} else
		{
			dts.addDeviceType(deviceType, line);
		}
		for (DeviceTypeAttr deviceTypeAttr : deviceType.getAttrList())
		{
			deviceTypeAttr.setTid(deviceType.getTid());
			deviceTypeAttr.setDeviceName(deviceType.getDeviceName());
		}
		dtas.deleteAttrByTypeId(deviceType.getTid(), line);
		int resultNum = dtas.addDeviceAttr(deviceType.getAttrList(), line);
		if (resultNum < 0)
		{
			mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			mb.setMessage("添加失败");
		} else
		{
			mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			mb.setMessage("添加成功");
		}
		return mb;
	}
	
	@RequestMapping(value = "deleteDeviceType")
	@ResponseBody
	public MessageBean deleteRow(HttpServletRequest req) {
		String ids = req.getParameter("tids");
		String[] ArrayIds = ids.split(",");
		String line = (String) req.getSession().getAttribute("LineID");
		MessageBean mb = new MessageBean();
		int resultNum = dts.deleteDeviceType(ArrayIds,line);
		if (resultNum < 0)
		{
			mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			mb.setMessage("删除失败");
		} else
		{
			mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			mb.setMessage("删除成功");
		}
		return mb;
	}
}
